package com.plant.consult.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import com.plant.consult.domain.*;
import com.plant.consult.mapper.ConsultDetailMapper;
import com.plant.consult.mapper.ConsultMapper;
import com.plant.consult.mapper.ConsultRecordMapper;
import com.plant.consult.service.ConsultDetailService;
import lombok.ToString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class ConsultDetailServiceImpl implements ConsultDetailService {
    @Autowired
    private ConsultDetailMapper consultDetailMapper;
    @Autowired
    private ConsultMapper consultMapper;
    @Autowired
    private ConsultRecordMapper consultRecordMapper;
    @Override
    public void insert(ConsultDetail consultDetail) {
        if(consultDetail.getSupers()==null){
            consultDetail.setSupers(0);
        }
        consultDetail.setCreateTime(new Date());
        consultDetail.setDel(0);
        consultDetailMapper.insert(consultDetail);
    }

    @Override
    @Transactional
    public void replyAdd(ConsultDetailDto consultDetailDto) {
//        创建对象
        ConsultDetail consultDetail = new ConsultDetail();
//        将dto里面与consultDetail相同的属性复制过去
        BeanUtil.copyProperties(consultDetailDto,consultDetail);
//        回复时间
        consultDetail.setCreateTime(new DateTime());
        consultDetail.setDel(0);
//        添加主表
        System.out.println(consultDetail);
        consultDetailMapper.replyAdd(consultDetail);
//        查询主表id
        Integer detailId = consultDetailMapper.selectInsert(consultDetail);
        System.out.println(detailId);
//        插入图片表
        consultDetailDto.setDetailId(detailId);
        consultDetailMapper.insertImg(consultDetailDto);

    }

    @Override
    public List<ConsultDetail> ReplyById(Consult consult) {
        ConsultRecord consultRecord = consultRecordMapper.findConId(consult.getConId());
        List<ConsultDetail> consultDetails = consultDetailMapper.findConId(consultRecord.getId());
        List<ConsultDetail> list = consultDetails.stream().filter(item -> item.getSupers() == 0).toList();
        for (ConsultDetail item : list) {
            List<ConsultDetail> replyList = consultDetails.stream().filter(item1 -> item1.getSupers() == item.getId()).toList();
            item.setConsultDetail(replyList);
        }
        return list;
    }


}
